// pages/danger/dangerTaskList/dangerTaskList.js
var app = getApp()
var util = require('../../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        taskList: [],
        taskDefineKey: 'usertask2',
        unCheckUrl: '',//待检查跳转的url
        reformSheetUrl: '',//发布整改单的url
        reformUrl: "",//整改的url
        checkAccept: "",//验收跳转的url
        taskListTitle:"",//列表的标题

        page: 1,
        reachBottom: false, //表示已经没有跟多的数据了
        showNoDataNotice: false//当没有数据时显示
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.taskDefineKey) {
            this.setData({
                taskDefineKey: options.taskDefineKey,
                taskListTitle:options.taskListTitle
            })
        }
        this.loadTaskList()

    },
    /**
     * 加载对应的任务
     */
    loadTaskList() {
        wx.showLoading({
            title: '加载中....',
        })
        var that = this
        var header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") //读取cookie
        };
        wx.request({
            header: header,
            method: 'GET',
            url: app.globalData.baseUrl + '/safe/dangerFlow/getUndoTask',
            data: {
                taskDefineKey: this.data.taskDefineKey,
            },
            success(res) {
                wx.hideLoading()
                if (res.data.success) {

                    var taskList = res.data.data.data
                    if (taskList.length > 0) {
                        for (var i = 0; i < taskList.length; i++) {
                            taskList[i].createTimeStr = util.formatTime(new Date(taskList[i].createTime), 'Y/M/D')
                        }
                        that.setData({
                            taskList: taskList,
                            showNoDataNotice: false
                        })
                    } else {
                        that.setData({
                            showNoDataNotice: true
                        })
                    }

                }
            }
        })

    },
    /**
     * 进入发布的隐患自查
     */
    inspectPublic: function (e) {
        var inspectPublicId = e.currentTarget.dataset.inspectpublicid
        wx.navigateTo({
            url: '/pages/danger/selfAdd/index?inspectId=' + inspectPublicId + '&inspect=false',
        })
    },

    jump: function (e) {
        var taskId = e.currentTarget.dataset.taskid
        var inspectType = e.currentTarget.dataset.inspecttype
        if (this.data.taskDefineKey == 'usertask2') {//检查
            var inspectPublicId = e.currentTarget.dataset.inspectpublicid

            wx.navigateTo({
                url: '/pages/danger/selfAdd/index?inspectId=' + inspectPublicId + '&inspect=false&taskId=' + taskId
                + '&inspectType=' + inspectType,
            })
        }
        if (this.data.taskDefineKey == 'usertask4') {//发布整改单
            var inspectPublicId = e.currentTarget.dataset.inspectpublicid
            var taskId = e.currentTarget.dataset.taskid
            var inspectName = e.currentTarget.dataset.inspectname

            wx.navigateTo({
                url: '/pages/danger/selfMakeList/selfMakeList?inspectId=' + inspectPublicId + '&taskId=' + taskId + '&inspectName=' + inspectName + '&inspectType=' + inspectType,
            })
        }
        if (this.data.taskDefineKey == 'usertask7') {//整改
            var inspectPublicId = e.currentTarget.dataset.inspectpublicid
            var inspectName = e.currentTarget.dataset.inspectname
            wx.navigateTo({
                url: '/pages/danger/dangerReform/index?inspectId=' + inspectPublicId + '&inspect=false&taskId=' + taskId + '&inspectName=' + inspectName + '&inspectType=' + inspectType,
            })
        }
        if (this.data.taskDefineKey == 'usertask9') {//验收
            var inspectPublicId = e.currentTarget.dataset.inspectpublicid
            wx.navigateTo({
                url: '/pages/danger/checkAccept/checkAccept?inspectId=' + inspectPublicId + '&inspect=false&taskId=' + taskId + '&inspectType=' + inspectType,
            })
        }
    },
    /**
     * 返回首页
     */
    backIndex(){
        wx.redirectTo({
            url:'/pages/index/index'
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            page:1
        })
        this.loadTaskList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        // this.loadTaskList()
        var that = this;
        // 显示加载图标
        wx.showLoading({
            title: '玩命加载中',
        })
        var header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") //读取cookie
        };
        var params = {
            pageCurrent: this.data.page + 1,
            taskDefineKey: this.data.taskDefineKey,
        }
        wx.request({
            header: header,
            method: 'GET',
            url: app.globalData.baseUrl + '/safe/dangerFlow/getUndoTask',
            data: params,
            success(res) {
                wx.hideLoading()
                if (that.data.reachBottom) {
                    wx.showToast({
                        title: '没有更多数据',
                    })
                } else {
                    if (res.data.success) {
                        var newTaskList = res.data.data.data
                        if (newTaskList.length <= 0) {
                            wx.showToast({
                                title: '没有更多数据',
                            })
                        }
                        var oldTaskList = that.data.taskList
                        for (var i = 0; i < newTaskList.length; i++) {
                            newTaskList[i].createTimeStr = util.formatTime(new Date(newTaskList[i].createTime), 'Y/M/D')
                            oldTaskList.push(newTaskList[i])
                        }
                        that.setData({
                            taskList: oldTaskList,
                            page: that.data.page + 1
                        })
                        if (res.data.page.pageCurrent < res.data.page.pages) {
                            that.setData({
                                reachBottom: true
                            })
                        }
                    }
                }
            }
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 根据状态值获取状态名
     */
    getInspectStatusName(status) {
        var statusName = ""
        switch (status) {
            case 1:
                statusName = "审核"
                break;
            case 2:
                statusName = "修改检查单"
                break;
            case 3:
                statusName = "待检查"
                break;
            case 4:
                statusName = "发布整改单"
                break;
            case 5:
                statusName = "隐患评估"
                break;
            case 6:
                statusName = "审核"
                break;
            case 7:
                statusName = "整改"
                break;
            case 8:
                statusName = "整改审核"
                break;
            case 9:
                statusName = "验收"
                break;
            case 10:
                statusName = "督办整改"
                break;
            case 11:
                statusName = "督办整改审核"
                break;
            case 12:
                statusName = "督办验收"
                break;
            case 13:
                statusName = "督办验收审核"
                break;
            case 14:
                statusName = "整改验收审核"
                break;
            case 15:
                statusName = "结束"
                break;
            default:
                statusName = "未知状态"
        }
        return statusName
    },
})